<?php

namespace Home\Controller;

//use Think\Controller;

class QuestionsController extends ExtendController {

    //显示栏目
    public function index() {
      if($_GET){
        $map['q_sid'] = $_GET['id'];
        $Questions = D('Questions');
        import('ORG.Util.Page'); // 导入分页类
        $count = $Questions->where($map)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();     // 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $Questions->where($map)->order('q_id DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        if($list){
              $this->assign('data',$map);
              $this->assign('list', $list); // 赋值数据集
              $this->assign('page', $show); // 赋值分页输出
              $this->display(); // 输出模板
            }else{
              $this->error('该科目还没有面试题 请添加!',U('add',array('id'=>$map['q_sid'])));
            }

      }else{

      }

    }
   
    //添加
    public function add() {
        if(!empty($_POST)){
          $Subject = M('Subject');
          $Questions = D('Questions');
          $map['name'] = $_POST['subject'];
          $res = $Subject->where($map)->field('id')->find();
          $data['q_content'] = $_POST['question_content'];
          $data['q_type_a'] = $_POST['type_a'];
          $data['q_type_b'] = $_POST['type_b'];
          $data['q_type_c'] = $_POST['type_c'];
          $data['q_type_d'] = $_POST['type_d'];
              $map = array();
            $map['q_type_e']  = $_POST['type_E'];
             $map['q_type_f']  = $_POST['type_F'];
             $map['q_type_g']  = $_POST['type_G'];
             $map['q_type_h']  = $_POST['type_H'];
             $map['q_type_i']  = $_POST['type_I'];
             $map['q_type_j']  = $_POST['type_J'];
             $map['q_type_k']  = $_POST['type_K'];
             $map['q_type_l']  = $_POST['type_L'];
             $map['q_type_m']  = $_POST['type_M'];
             $map['q_type_n']  = $_POST['type_N'];
          $data['q_type_e'] = json_encode($map);
          $data['q_answer'] = $_POST['answer'];
          $data['q_img'] = $_POST['image_url'];
          $data['date'] = date("Y-m-d ");
          if($_POST['type_a'] == '' && $_POST['type_b'] == ''  ){
            $data['q_type'] = 2 ;
          }else{
            $data['q_type'] = 1 ;
          }
          $data['q_sid'] = $res['id'];
          $res = $Questions->add($data);
          if($res){
              $this->redirect('index?id='.$data['q_sid']);
          }else{
            $this->error('添加失败!');
          }
         }else{
          if (!empty($_GET)) {
             $map['id'] = $_GET['id'];
            $Subject = M('Subject');
            $data = $Subject->where($map)->find();
            $this->assign('data',$data);
            $this->display('insert_questions');
          }
 
    }
  }
      //修改
     public function save() {
      if(empty($_POST)){
             $map['id'] = $_GET['sid'];
             $row['q_id'] = $_GET['id'];
            $Subject = M('Subject');
            $Questions = M('Questions');
            $data = $Subject->where($map)->find();
            $res = $Questions->where($row)->find();
               $i = json_decode($res['q_type_e'],true);
            if($res){
                   $this->assign('questions',$i);
                  $this->assign('list',$res);
                  $this->assign('data',$data);
                  $this->display('updata_questions');
            }else{
              $this->error('非法操作');
            }
      }else{
          $Questions = M('Questions');
          $m['q_id'] = $_POST['q_id'];
           $data['q_content'] = $_POST['question_content'];
          $data['q_type_a'] = $_POST['type_a'];
          $data['q_type_b'] = $_POST['type_b'];
          $data['q_type_c'] = $_POST['type_c'];
          $data['q_type_d'] = $_POST['type_d'];
            $map = array();
            $map['q_type_e']  = $_POST['type_E'];
             $map['q_type_f']  = $_POST['type_F'];
             $map['q_type_g']  = $_POST['type_G'];
             $map['q_type_h']  = $_POST['type_H'];
             $map['q_type_i']  = $_POST['type_I'];
             $map['q_type_j']  = $_POST['type_J'];
             $map['q_type_k']  = $_POST['type_K'];
             $map['q_type_l']  = $_POST['type_L'];
             $map['q_type_m']  = $_POST['type_M'];
             $map['q_type_n']  = $_POST['type_N'];
          $data['q_type_e'] = json_encode($map);
          $data['q_answer'] = $_POST['answer'];
          $data['q_img'] = $_POST['image_url'];
          $res = $Questions->where($m)->save($data);
          if(!empty($res)){
            $this->redirect('index?id='.$_POST['q_sid']);
          }else{
            $this->error('修改失败!');
          }
      }

    }

    
    //删除
    public function delete() {
        $Questions = M('Questions');
        $map['q_id'] = $_GET['id'];
        $result = $Questions->where($map)->delete();
        if ($result != false) {
            $this->redirect('index?id='.$_GET['sid']);
        } else {
            $this->error('删除失败!');
        }
    }

}